  <script setup lang='ts'>
  import { articleAPI, articleDeleteAPI, articleGetChannelAPI} from '@/apis/article';
  import type { channel_list, articleParams, Result } from '@/types/article';
  import { ArrowRight, Delete, EditPen } from '@element-plus/icons-vue';
  import { onMounted, ref, watch } from 'vue';
  const params = ref<articleParams>({
    status: undefined,
    channel_id: '',
    begin_pubdate: '',
    end_pubdate: '',
    page: 1,
    per_page: 10
  })
  const tableData = ref<Result[]>()
  const channel = ref<channel_list[]>([])
  const date = ref<string[]>([])
  onMounted(() => {
    articleGetChannelAPI().then(res => {
      channel.value = res.data.channels
    })
  })
  watch(date, () => {
    if (date.value) {
      params.value.begin_pubdate = date.value[0]
      params.value.end_pubdate = date.value[1]
    }
    else {
      params.value.begin_pubdate = ''
      params.value.end_pubdate = ''
    }
  }, { deep: true })
  const total = ref(0)
  const onFilter = async () => {
    const res = await articleAPI(params.value)
    total.value = res.data.total_count
    tableData.value = res.data.results
  }
  onFilter()
  const onDel = async(id:number) => {
    await articleDeleteAPI(id)
    onFilter()
  }

</script>
<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <el-breadcrumb :separator-icon="ArrowRight">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </template>
    <el-form label-width="80px" :model="params">
      <el-form-item label="状态：">
        <el-radio-group v-model="params.status">
          <el-radio :value="undefined">全部</el-radio>
          <el-radio :value="0">草稿</el-radio>
          <el-radio :value="1">待审核</el-radio>
          <el-radio :value="2">审核通过</el-radio>
          <el-radio :value="3">审核失败</el-radio>
          <el-radio :value="4">已删除</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="频道：">
        <el-select placeholder="请选择" size="large" style="width: 240px" v-model="params.channel_id" clearable>
          <el-option v-for="item in channel" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期：">
        <div class="block">
          <el-date-picker type="daterange" range-separator="To" start-placeholder="Start date"
            end-placeholder="End date" size="large" v-model="date" />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onFilter">筛选</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin-top: 20px;">
    <template #header>
      <div class="card-header">
        <span>根据筛选条件共查询到{{ total }}条结果：</span>
      </div>
    </template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="cover" label="封面">
        <template #default="scope">
          <img :src="scope.row.cover.images[0]" alt="" style="width: 100px;height: 100px;">
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
        <el-tag v-if="scope.row.status===0" type="info">草稿</el-tag>
        <el-tag v-if="scope.row.status===1" type="warning">待审核</el-tag>
        <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
        <el-tag v-if="scope.row.status===3" type="danger">审核失败</el-tag>
        <el-tag v-if="scope.row.status===4" type="info">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="pubdate" label="发布时间" />
      <el-table-column prop="read_count" label="阅读数" />
      <el-table-column prop="comment_count" label="评论数" />
      <el-table-column prop="like_count" label="点赞数" />
      <el-table-column label="操作">
        <template #default={row}>
          <el-button type="primary" :circle="true" size="large" @click="$router.push(`/publish/${row.id}`)"><el-icon><EditPen /></el-icon></el-button>
          <el-button type="danger" :circle="true" size="large" @click="onDel(row.id)"><el-icon><Delete /></el-icon></el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <el-pagination
      :page-sizes="[2, 5, 10, 20]"
      size="large"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      v-model:page-size="params.per_page"
      v-model:current-page="params.page"
      @change="onFilter"
    />
    </template>
  </el-card>
</template>
<style></style>